package com.bing.main;

import com.bing.Constant;
import com.bing.Utils;
import com.bing.entity.Account;
import com.bing.entity.Question;
import com.bing.services.*;
import com.bing.task.v1.AccountResult;
import com.bing.task.v1.FetchAndParseNoteReview;
import com.bing.task.v1.FetchParseQuestionAid;
import com.bing.task.v1.QuestionResult;
import com.bing.task.v1.UpdateAccountDb;
import com.bing.task.v1.UpdateQuestionDb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;

/**
 * Created by kaibb on 15-11-12.
 */
public class MyFengWoMain {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyFengWoMain.class);

    private static final QuestionServices questionServices = new QuestionsServicesImpl();
    private static final AccountServices accountServices = new AccountServicesImpl();
    private static final ReviewServices reviewServices = new ReviewServicesImpl();
    private static final NoteServices noteServices = new NoteServicesImpl();
    private static final AnswerServices answerServices = new AnswerServicesImpl();
    private static final ExecutorService SERVICE = Executors.newFixedThreadPool(4);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        Utils.initPath();
        Utils.initCookies();
       /* if (questionServices.getTotalQuestion() != Constant.TOTAL_QUESTIONS) {
            LOGGER.info("初始化问题表!");
            questionServices.initQuestion();
            LOGGER.info("结束初始化问题表!");
        }*/
          new Thread(new FPQuestion()).start();
       // new Thread(new FPAccount()).start();

    }

    static class FPQuestion implements Runnable {

        @Override
        public void run() {
            ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());

            LOGGER.info("开始抓取问题答案到数据库中.........................");
            for (int i = 0; ; i++) {
                LOGGER.info("第{}页数据", i);
                try {
                    List<Question> taskList = questionServices.getNoAidQuestions(Constant.PAGE_SIZE);
                    QuestionResult questionResult = forkJoinPool.submit(new FetchParseQuestionAid(taskList)).get();
                    SERVICE.submit(new UpdateQuestionDb(questionResult, questionServices, accountServices, answerServices));
                    if (taskList.size() != Constant.PAGE_SIZE) {
                        break;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
            forkJoinPool.shutdown();
            LOGGER.info("结束抓取问题答案到数据库中.........................");
        }
    }


    static class FPAccount implements Runnable {

        @Override
        public void run() {
            ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()+3);

            LOGGER.info("开始点评游记答案到数据库中.........................");
            for (int i = 0; ; i++) {
                LOGGER.info("第{}页数据", i);
                try {
                    List<Account> taskList = accountServices.getAccounts(Constant.PAGE_SIZE);
                    AccountResult accountResult = forkJoinPool.submit(new FetchAndParseNoteReview(taskList)).get();
                    new UpdateAccountDb(accountResult, questionServices, accountServices, noteServices, reviewServices).run();
                    if (taskList.size() < Constant.PAGE_SIZE) {
                        LOGGER.info("暂停一会");
                        Thread.sleep(3 * 1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
            //            LOGGER.info("结束点评游记答案到数据库中.........................");
        }
    }

}
